package com.longtop.framework.sys.flow.web;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.flow.service.LeaveService;
import com.longtop.framework.util.DateUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;

/**
 * 请假流程--启动
 * 
 * @author 胡砥峰
 * 
 */
public class LeaveLaunchAction extends BaseAction {

	private final static String DATASET_FLOW_LEAVE = "Dataset_FlowLeave";

	/**
	 * JSP页面初始化方法
	 */
	public void init(IInitContext context) {
		super.init(context);

		// 获取页面上的dataset的结构,验证数据集对象是否存在
		Dataset leaveDataset = context.getDataset(DATASET_FLOW_LEAVE);
		if (!WebUtil.validateDatasetExist(this, leaveDataset, DATASET_FLOW_LEAVE)) {
			return;
		}

		// 当前SessionObject对象
		SessionObject sessionObject = this.getSessionObject(context);

		leaveDataset.append();
		leaveDataset.setValue("applyDate", DateUtil.getDateTime());
		leaveDataset.setValue("applyMan", sessionObject.getUserName());
	}

	public void save(IActionContext context) {
		try {
			LeaveService leaveService = this.getBean(LeaveService.BEAN_ID);

			SessionObject sessionObject = this.getSessionObject(context); // session

			leaveService.saveFlowLaunch(context, "leave", sessionObject);

			// 向JSP页面传递操作结果信息
			this.hintInfoMessage("请假流程启动成功");
		} catch (BaseCheckedException e) {
			// 在界面显示错误信息
			this.hintErrorMessage(e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}